const http=require('http');
const url=require('url');
const fs=require('fs');
const os=require('os');
const cluster=require('cluster');
const process=require('process');
const stream=require('stream');
const zlib=require('zlib');


if(cluster.isMaster){
	for(let i=0;i<os.cpus().length;i++){//cpu有4核就会分裂4个子进程
		cluster.fork();//主进程里只负责分裂
	}
	console.log('主进程')
}else{
	
	http.createServer((req,res)=>{
		let {pathname,query}=url.parse(req.url,true);

		let rs=fs.createReadStream(`./${pathname}`);
		rs.pipe(res);
		rs.on('error',err=>{
			if(err){
				res.writeHeader(404);
				res.write('not found');
				res.end();
			}
		});
		console.log(`工作进程${process.pid}`)//主子进程可以共享端口

	}).listen(8080);
}

